package com.ktjy.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjy.dto.ActivityDTO;
import com.ktjy.dto.CourseDTO;
import com.ktjy.pojo.SysUser;
import com.ktjy.pojo.TbActivity;
import com.ktjy.pojo.TbCourse;
import com.ktjy.service.ITbActivityService;
import com.ktjy.utils.AjaxResult;
import com.ktjy.utils.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 活动管理 前端控制器
 * </p>
 *
 * @author jjx
 * @since 2025-08-02
 */
@RestController
@RequestMapping("/api/tb/activity")
@Tag(name = "活动管理模块")
public class TbActivityController {
    Logger log = LoggerFactory.getLogger(TbActivityController.class);

    @Autowired
    ITbActivityService activityService;

    @GetMapping("/list")
    @Operation(summary = "活动列表接口")
    @PreAuthorize("@ss.hasPerm('clues:activity:list')")
    public AjaxResult list(@Param("activityDTO") ActivityDTO activityDTO,
                           @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        IPage<TbActivity> page = activityService.findPage(activityDTO, pageNo, pageSize);
        return AjaxResult.success(page);
    }
    //查询全部
    @GetMapping("/listAll")
    @Operation(summary = "查询全部接口")
    public AjaxResult listAll() {
        List<TbActivity> list = activityService.list();
        return AjaxResult.success(list);
    }

    @PostMapping("/save")
    @PreAuthorize("@ss.hasPerm('clues:activity:add') or @ss.hasPerm('clues:activity:edit')")
    @Operation(summary = "活动保存接口")
    public AjaxResult saveOrUpdate(@RequestBody TbActivity tbActivity) {
        try {
            if (tbActivity.getId() == null) {
                String lowerCase = RandomStringUtils.randomAlphanumeric(8).toLowerCase(); //课程编号
                tbActivity.setCode(lowerCase);
            }
            boolean ok = activityService.saveOrUpdate(tbActivity);
            if (ok) {
                return AjaxResult.success("保存成功");
            }
        } catch (Exception e) {
            log.error("TbActivityController中saveOrUpdate方法执行报错的。", e);
        }
        return AjaxResult.error("保存失败");
    }

    @GetMapping("/view/{id}")
    @Operation(summary = "活动详情接口")
    @PreAuthorize("@ss.hasPerm('clues:activity:query') or @ss.hasPerm('clues:activity:edit')")
    public AjaxResult view(@PathVariable("id") Long id) {
        //查询详情
        try {
            TbActivity tbActivity = activityService.getById(id);
            if (tbActivity != null) {
                return AjaxResult.success(tbActivity);
            }
        } catch (Exception e) {
            log.error("TbActivityController中view方法执行报错的。", e);
        }
        return AjaxResult.error();
    }

    @DeleteMapping("/del/{id}")
    @Operation(summary = "活动删除接口")
    @PreAuthorize("@ss.hasPerm('clues:activity:remove')")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            boolean ok = activityService.removeById(id);
            if (ok) {//删除成功
                return AjaxResult.success("删除成功");
            }
        } catch (Exception e) {
            log.error("TbActivityController中del方法执行报错的。", e);
        }
        return AjaxResult.error("删除失败");
    }

    @PutMapping("/pass")
    @PreAuthorize("@ss.hasPerm('clues:activity:pass')")
    @Operation(summary = "活动通过接口")
    public AjaxResult pass(@RequestBody TbActivity tbActivity) {
        try {
            boolean ok = activityService.updateById(tbActivity);
            if (ok) {
                return AjaxResult.success();
            }
        } catch (Exception e) {
            log.error("TbActivityController中pass方法执行报错的。", e);
        }
        return AjaxResult.error();
    }

    @PutMapping("/reject")
    @PreAuthorize("@ss.hasPerm('clues:activity:reject')")
    @Operation(summary = "活动驳回接口")
    public AjaxResult reject(@RequestBody TbActivity tbActivity) {
        try {
            boolean ok = activityService.updateById(tbActivity);
            if (ok) {
                return AjaxResult.success();
            }
        } catch (Exception e) {
            log.error("TbActivityController中reject方法执行报错的。", e);
        }
        return AjaxResult.error();
    }

    @GetMapping("/export")
    @PreAuthorize("@ss.hasPerm('clues:activity:export')")
    public void exportExcel(ActivityDTO activityDTO, HttpServletResponse response) {
        //1. 查询需要导出的数据
        List<TbActivity> list = activityService.finList(activityDTO);
        ExcelUtil.exportExcel(list, "活动数据", TbActivity.class, response);
    }
}
